package com.gugee.ins.data.task.media.service;


import com.gugee.ins.common.dal.service.brand.CommonBrandDao;
import com.gugee.ins.common.dal.service.brand.InsBrandDao;
import com.gugee.ins.common.dal.service.brand.InsRelBrandMediaDao;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.common.model.mysql.brand.CommonBrand;
import com.gugee.ins.common.model.mysql.brand.InsRelBrandMedia;
import com.gugee.ins.common.model.mysql.statistics.InsStBloggerRealFollowing;
import com.gugee.ins.data.task.config.TaskConfig;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.groovy.transform.trait.TraitASTTransformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class InsBrandMediaRelSvc {

    @Autowired
    CommonBrandDao commonBrandDao;

    @Autowired
    InsRelBrandMediaDao insRelBrandMediaDao;

    @Autowired
    public TaskConfig taskConfig;

    @Autowired
    InsBrandDao  insBrandDao;


    public ReturnT<String> xxlJobStart(){
        long start = System.currentTimeMillis();
        try {
            log.info("品牌相关帖子收集 开始");
            XxlJobLogger.log("品牌相关帖子收集 开始");
            handler();
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            ex.printStackTrace();
            return ReturnT.FAIL;
        }finally {
            log.info( "品牌相关帖子收集 结束, cost total={}", (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log( "品牌相关帖子收集 结束, cost total={}",(System.currentTimeMillis() - start) / 1000);
        }
    }

    public void handler(){
        List<CommonBrand> commonBrands = commonBrandDao.getByAfterId(0,taskConfig.getInsBloggerMarkDayPageSize());
        // List<CommonBrand> commonBrands = commonBrandDao.getByAfterId(120335,1);

        while(commonBrands.size() > 0){

            commonBrands.forEach(c->{
                try{
                    List<InsBloggerMain> bloggerMains = insBrandDao.getBrandBloggerByBid(c.getId());

                    if(bloggerMains.size() > 0){
                        getMedias(c,bloggerMains);
                    }
                }catch (Exception e){
                    log.error("收集{}帖子异常",c.getEnName(),e);
                    log.info("收集{}帖子异常",c.getEnName(),e);
                }
            });
            commonBrands = commonBrandDao.getByAfterId(commonBrands.get(commonBrands.size()-1).getId(),taskConfig.getInsBloggerMarkDayPageSize());
        }
    }

    private void getMedias(CommonBrand commonBrand,List<InsBloggerMain> bloggerMains){
        long start = System.currentTimeMillis();
        long timeRang = System.currentTimeMillis() / 1000 - taskConfig.getLatestDays() * 24 * 3600;

        List<String> usernames = new ArrayList<>(bloggerMains.size());
        List<Long> uIds = new ArrayList<>(bloggerMains.size());

        bloggerMains.forEach( b->{
            usernames.add(b.getUsername());
            uIds.add(b.getUid());
        });
        List<InsRelBrandMedia> brandMediaAll = new ArrayList<>();
        // 获取@品牌下博主的帖子
        brandMediaAll.addAll(insBrandDao.getMediaForCaptionTagger(usernames,commonBrand.getId(),timeRang,System.currentTimeMillis() / 1000));
        // 获取标记品牌下博主的帖子
        brandMediaAll.addAll(insBrandDao.getMediaForTaggerUser(uIds,commonBrand.getId(),timeRang,System.currentTimeMillis() / 1000));
        // 获取品牌下博主赞助的帖子
        brandMediaAll.addAll(insBrandDao.getMediaForSponsorUser(uIds,commonBrand.getId(),timeRang,System.currentTimeMillis() / 1000));
        // 获取品牌下的博主下的商品被标记的帖子
        brandMediaAll.addAll(insBrandDao.getMediaForProduct(uIds,commonBrand.getId(),timeRang,System.currentTimeMillis() / 1000));

        // 标记品牌下的博主帖子
        brandMediaAll.stream().forEach( b ->{
            if(uIds.contains(b.getOwnerUid())){
                b.setIsBrandAccount(true);
            }
        });

        // 获取品牌下的博主下的帖子
        brandMediaAll.addAll(insBrandDao.getMediaForBlogger(uIds,commonBrand.getId(),timeRang,System.currentTimeMillis() / 1000));

        Set<InsRelBrandMedia> insRelBrandMediaSet = new HashSet<>(brandMediaAll);
        brandMediaAll = new ArrayList<>(insRelBrandMediaSet);
        insRelBrandMediaDao.delAndInsert(commonBrand.getId(),brandMediaAll,taskConfig.getInsBloggerMarkDayPageSize());
        log.info( "{}相关帖子收集 结束, cost total={}",commonBrand.getEnName(), (System.currentTimeMillis() - start) / 1000);
    }
}
